<?php
// 定义游戏棋盘的行数、列数和地雷数量
$rows = 9;
$cols = 9;
$mines = 10;

// 初始化游戏棋盘数组
$board = array_fill(0, $rows, array_fill(0, $cols, 0));

// 随机放置地雷
$mineCount = 0;
while ($mineCount < $mines) {
    $row = rand(0, $rows - 1);
    $col = rand(0, $cols - 1);
    if ($board[$row][$col] === 0) {
        $board[$row][$col] = -1;
        $mineCount++;
    }
}

// 计算每个非地雷格子周围的地雷数量
for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j < $cols; $j++) {
        if ($board[$i][$j] !== -1) {
            $count = 0;
            for ($x = max(0, $i - 1); $x <= min($i + 1, $rows - 1); $x++) {
                for ($y = max(0, $j - 1); $y <= min($j + 1, $cols - 1); $y++) {
                    if ($board[$x][$y] === -1) {
                        $count++;
                    }
                }
            }
            $board[$i][$j] = $count;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>扫雷游戏</title>
    <style>
        table {
            border-collapse: collapse;
        }

        td {
            width: 20px;
            height: 20px;
            border: 1px solid #000;
            text-align: center;
            cursor: pointer;
        }

        .revealed {
            background-color: #ccc;
        }

        .mine {
            background-color: red;
        }
    </style>
</head>

<body>
    <table id="game-board">
        <?php
        for ($i = 0; $i < $rows; $i++) {
            echo "<tr>";
            for ($j = 0; $j < $cols; $j++) {
                echo "<td data-row='$i' data-col='$j'></td>";
            }
            echo "</tr>";
        }
        ?>
    </table>
    <script>
        const board = <?php echo json_encode($board); ?>;
        const rows = <?php echo $rows; ?>;
        const cols = <?php echo $cols; ?>;
        const mines = <?php echo $mines; ?>;
        const gameBoard = document.getElementById('game-board');
        const cells = gameBoard.getElementsByTagName('td');
        let gameOver = false;

        function revealCell(row, col) {
            if (gameOver || row < 0 || row >= rows || col < 0 || col >= cols) {
                return;
            }
            const cell = gameBoard.rows[row].cells[col];
            if (cell.classList.contains('revealed')) {
                return;
            }
            cell.classList.add('revealed');
            const value = board[row][col];
            if (value === -1) {
                // 踩到地雷，游戏结束
                gameOver = true;
                for (let i = 0; i < rows; i++) {
                    for (let j = 0; j < cols; j++) {
                        if (board[i][j] === -1) {
                            gameBoard.rows[i].cells[j].classList.add('mine');
                        }
                    }
                }
                alert('游戏结束，你踩到地雷了！');
            } else if (value === 0) {
                // 周围没有地雷，递归展开周围的格子
                for (let x = Math.max(0, row - 1); x <= Math.min(row + 1, rows - 1); x++) {
                    for (let y = Math.max(0, col - 1); y <= Math.min(col + 1, cols - 1); y++) {
                        revealCell(x, y);
                    }
                }
            } else {
                cell.textContent = value;
            }
        }

        for (let i = 0; i < cells.length; i++) {
            cells[i].addEventListener('click', function () {
                const row = parseInt(this.dataset.row);
                const col = parseInt(this.dataset.col);
                revealCell(row, col);
            });
        }
    </script>
</body>

</html>